﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Customer_Profile.eCardReport.DBDao;
using KVB.Common;
using System.Text;
using Customer_Profile.eCard.DBDao;
using DevExpress.Web.ASPxGridView;

namespace Customer_Profile.eCardReport
{
    public partial class DairyMaturityReport : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DataSet dsProd = ProductDao.getAllProduct("ALL");
                dropProd.DataSource = dsProd;
                dropProd.DataTextField = "procduct_name";
                dropProd.DataValueField = "procduct_code";
                dropProd.DataBind();
                ListItem liAllprod = new ListItem();
                liAllprod.Text = "All";
                liAllprod.Value = "All";
                dropProd.Items.Insert(0, liAllprod);
            
                txtgedatebegin.Text = DateTime.Now.ToString("yyyy-MM-dd");
                DateTime dt = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(1).AddDays(-1);
                txtgedateend.Text = dt.ToString("yyyy-MM-dd");
            }
            BindDataView();
            int colCount = ASPxGridView1.Columns.Count;
            for (int i = 1; i < colCount; i++)
            {
                ((GridViewDataColumn)ASPxGridView1.Columns[i]).PropertiesEdit.DisplayFormatString = "c";
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {

        }

        private void BindDataView()
        {
            string startDate = txtgedatebegin.Text.Trim();
            string endDate = txtgedateend.Text.Trim();
            string strSQL1 = "";
            string strSQL2 = "";
            try
            {
                strSQL1 = "select '总计：' '产品',SUM(case when status='PASS' AND if_returncard='N' then due_amount else 0 end) as 'Total:',";
                strSQL2 = "select (SELECT procduct_name FROM dbo.prod_type WHERE procduct_code=d.prod_code) prod_code,SUM(case when status='PASS' AND if_returncard='N' then due_amount else 0 end) as 'Total:',";
                DataSet dsRefDate = ReportDao.getAllRefDate(startDate, endDate);
                foreach (DataRow dr in dsRefDate.Tables[0].Rows)
                {
                    strSQL1 += "SUM(case when convert(date,due_date) between '" + dr["ref_date"] + "' and '" + dr["ref_date"] + "' then due_amount else 0 end ) '" + dr["date_remark"] + "' ,";
                    strSQL2 += "SUM(case when convert(date,due_date) between '" + dr["ref_date"] + "' and '" + dr["ref_date"] + "' then due_amount else 0 end ) '" + dr["date_remark"] + "' ,";
                }
                strSQL1 = strSQL1.Substring(0, strSQL1.Length - 1);
                strSQL2 = strSQL2.Substring(0, strSQL2.Length - 1);
                strSQL1 += " from mmdeals d where convert(date,due_date) between '" + startDate + "' and '" + endDate + "' and status='PASS' AND if_returncard='N' union all ";
                strSQL2 += " from mmdeals d where convert(date,due_date) between '" + startDate + "' and '" + endDate + "' and status='PASS' AND if_returncard='N' group by prod_code";
                //KVB.Common.Log.WriteDefaultLog("每日到期SQL:", strSQL1 + strSQL2);
                DataSet dsResult = ReportDao.DairySumReport(strSQL1 + strSQL2);
                if (dsResult != null && dsResult.Tables.Count > 0)
                {
                    ASPxGridView1.Columns.Clear(); //清除列
                    ASPxGridView1.AutoGenerateColumns = true; //设置自动添加列
                    ASPxGridView1.DataSourceID = null;
                    ASPxGridView1.DataSource = dsResult.Tables[0];
                    ASPxGridView1.DataBind();
                    ASPxGridView1.AutoGenerateColumns = false;
                }
                //else
                //{
                //    ASPxGridView1.DataBind();
                //}
                ASPxGridView1.Columns[0].Width = 150;
            }
            catch (Exception ex)
            {
                Log.WriteDefaultLog("Maturity_BindDataView", ex.ToString());
            }
        }

        protected void btn_Export_Click(object sender, EventArgs e)
        {
            //DevExpress.XtraPrinting.CsvExportOptions options = new DevExpress.XtraPrinting.CsvExportOptions();
            //options.TextExportMode = DevExpress.XtraPrinting.TextExportMode.Text;
            gridExport.WriteXlsToResponse("日销售统计"); //, options);
        }
    }
}